const app = getApp()
let commentContent = ""
Page({
    data: {
        current: 1,
        commentDesc: "说点儿什么"
    },

    onLoad: function (options) {
        this.setData({
            shareId: options.shareId,
            imgSize: (app.window.width - 69) / 3,
            mid: wx.getStorageSync("mid")
        })
        this.loadData()
    },

    onShow() {
        if (app.newMsg) {
            let key = this.getKey()
            const obj = this.data[key]
            obj.list.unshift(app.newMsg)
            app.newMsg = false
            this.setData({
                [key + ".list"]: obj.list
            })
        }
    },


    loadData() {
        let url = "shop-circle/circle-list"
        if (this.data.current == 2) {
            url = "shop-circle/keep-circle"
        }
        if (this.data.current == 3) {
            url = "member/collect"
        }
        const key = "obj" + this.data.current
        if (!this.data[key]) {
            this.setData({
                [key]: {
                    page: 1,
                    total: 2,
                    list: [],
                    loading: false
                }
            })
        }
        const obj = this.data[key]
        if (obj.page > obj.total || obj.loading) {
            return
        }
        this.setData({
            [key + ".loading"]: true
        })
        app.http.get(url, {
            data: { page: obj.page }
        }).then(res => {
            obj.loading = false
            if (res.data) {
                obj.page++
                obj.total = res.data.total
                obj.list.push(...res.data.items)
            }
            this.setData({
                [key]: obj
            })
            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(err => {
            console.log(err)
            app.wxToast.error(err.message)
            wx.stopPullDownRefresh()

        })
    },

    typeClick(evt) {
        const current = evt.currentTarget.dataset.current
        if (current != this.data.current) {
            this.setData({
                current
            })
            this.loadData()
        }
    },

    keep(evt) {
        const key = this.getKey()
        const { index, mid } = evt.currentTarget.dataset
        app.http.get('shop-circle/keep', {
            data: {
                mid: evt.currentTarget.dataset.mid
            }
        }).then(res => {
            this.setData({
                [`${key}.list[${index}].has_keep`]: !this.data[key].list[index].has_keep
            })
        }).catch(err => {

        })
    },

    getKey() {
        return "obj" + this.data.current
    },

    onPullDownRefresh: function () {
        const key = "obj" + this.data.current
        this.setData({
            [key]: {
                page: 1,
                total: 2,
                list: [],
                loading: false
            }
        })
        this.loadData()
    },

    makeComment(evt) {
        let { index, aid } = evt.currentTarget.dataset
        this.setData({
            isComment: true,
            commentDesc: "说点什么",
            article: {
                index,
                aid,
                pid: ""
            }
        })
    },
    closeComment() {
        this.setData({
            isComment: false,
            article: false
        })
    },
    inputComment(evt) {
        commentContent = evt.detail.value
    },
    submitComment() {
        const { index, aid, pid } = this.data.article
        app.http.post("shop-circle/create-comment", {
            data: {
                shop_circle_id: aid,
                content: commentContent,
                pid,
            }
        }).then(res => {
            const last = this.data[this.getKey()].list[index].shopCircleComments
            last.push(res.data)
            this.setData({
                [`${this.getKey()}.list[${index}].shopCircleComments`]: last,
                isComment: false,
                article: false
            })
        })
    },

    onReachBottom: function () {
        this.loadData()
    },

    onShareAppMessage: function () {

    },
    commentClick(evt) {
        const { comment, index, cindex, cid } = evt.currentTarget.dataset
        console.log(comment)
        this.setData({
            commentDesc: "回复 " + comment.member_name + " ："
        })
        if (comment.member_id == this.data.mid) {
            wx.showActionSheet({
                itemList: ["删除"],
                success: res => {
                    if (res.tapIndex == 0) {
                        app.http.post("shop-circle/circle-comment", {
                            data: { pid: comment.id }
                        }).then(res => {
                            const last = this.data[this.getKey()].list[index].shopCircleComments
                            last.splice(cindex, 1)
                            this.setData({
                                [`${this.getKey()}.list[${index}].shopCircleComments`]: last,
                            })
                        })
                    }
                }
            })
        } else {
            return
            this.setData({
                article: {
                    index,
                    aid: cid,
                    pid: comment.id
                },
                isComment: true
            })
        }
    },

    likeClick(evt) {
        const key = this.getKey()
        let { index, id } = evt.currentTarget.dataset
        if (this.data[key].list[index].has_like) {
            return
        }

        app.http.get('shop-circle/circle-like', {
            data: {
                id: id
            }
        }).then(res => {

            let c = this.data[key].list[index].lick_num;
            this.setData({
                [`${key}.list[${index}].has_like`]: true,
                [`${key}.list[${index}].lick_num`]: parseInt(c) + 1,
            });
        }).catch(err => {

        })
    },

    dropMsg(evt) {
        const { id, index } = evt.currentTarget.dataset
        wx.showActionSheet({
            itemList: ['删除'],
            success: res => {
                if (res.tapIndex == 0) {
                    app.http.get("shop-circle/delete-my-circle", {
                        data: {
                            id,
                        }
                    }).then(res => {
                        const last = this.data[this.getKey()].list
                        last.splice(index, 1)
                        this.setData({
                            [this.getKey() + ".list"]: last
                        })
                    })
                }
            }
        })
    },

    preImg(evt) {
        const { imgs, current } = evt.currentTarget.dataset
        wx.previewImage({
            urls: imgs.map(o => o.url),
            current,
        })
    },

    moreComment(evt) {
        app.moreComment = evt.currentTarget.dataset.item
        wx.navigateTo({
            url: './more-comment',
        })
    },
})